package net.coolcollege.operator;

import org.apache.activemq.command.ActiveMQQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

/**
 * activemq 发送器
 *
 * @author mengzhg
 */
public class ActiveMqSender implements Sender {

    private Logger logger = LoggerFactory.getLogger(ActiveMqSender.class);

    @Resource
    private JmsMessagingTemplate jmsTemplate;

    @Resource
    private MessageTranslator translator;

    @Override
    public boolean send(String content) {
        try {
            jmsTemplate.convertAndSend(new ActiveMQQueue("mqQueueName"), content);
        } catch (Exception e) {
            logger.error("发送消息到MQ出错");
            return false;
        }
        return true;
    }

    @Override
    public boolean send(String messageId, Locale locale, List<String> variables) {
        String finalContent = translator.translate(messageId, locale, variables);
        return send(finalContent);
    }
}
